package subraSoft.centaurus.gui.components;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JCheckBox;
import javax.swing.JPanel;



/**
 * Componente para permitir marcar la fecha actual.
 * 
 * @author caeycae
 *
 */
public class CenDateCheckBox extends JPanel implements ActionListener
{
	private static final long serialVersionUID = 1L;
	private JCheckBox check;
	private CenDateField dateDfl;
	
	public CenDateCheckBox(Boolean dateDflEnabled)
	{
		setLayout( new GridBagLayout() );
		GridBagConstraints destConst = new GridBagConstraints();
		destConst.fill = GridBagConstraints.HORIZONTAL;

		check = new JCheckBox();
		dateDfl = new CenDateField();
		dateDfl.setEnabled(dateDflEnabled);
		check.addActionListener(this);
		
		destConst.gridy = 0;
		destConst.gridx = 0;
		destConst.weightx = 0;
		add(check, destConst);
		
		destConst.gridy = 0;
		destConst.gridx = 1;
		destConst.weightx = 100;
		add(dateDfl, destConst);
	}

	public void actionPerformed(ActionEvent e) 
	{
		
		if(check.isSelected())
			dateDfl.setValue(new Date());
		else
			dateDfl.setValue(null);
	}
	
	public Date getDate()
	{
		return dateDfl.getDate();
	}
	
	public void setDate(Date date)
	{
		check.setSelected((date != null));			
		dateDfl.setDate(date);
	}

	@Override
	public void setEnabled(boolean enabled) 
	{
		dateDfl.setEnabled(enabled);
		check.setEnabled(enabled);
	}
	
	
}
